package com.zyguo.sdk.websocket;

import java.util.Arrays;
public class ArrayCopy {
                        
    public static <T> T[] concat(T[] first, T[] second) {
        T[] result = Arrays.copyOf(first, first.length + second.length);
        System.arraycopy(second, 0, result, first.length, second.length);
        return result;
    }
    
    public static byte[] concat(byte[] first, byte[] second) {
        byte[] result = Arrays.copyOf(first, first.length + second.length);
        System.arraycopy(second, 0, result, first.length, second.length);
        return result;
    }
                        
    public static <T> T[] concatAll(T[] first, T[]... rest) {
        int totalLength = first.length;
        for (T[] array : rest) {
            totalLength += array.length;
        }
        T[] result = Arrays.copyOf(first, totalLength);
        int offset = first.length;
        for (T[] array : rest) {
            System.arraycopy(array, 0, result, offset, array.length);
            offset += array.length;
        }
        return result;
    }
    
    public static byte[] concatAll(byte[] first, byte[]... rest) {
        int totalLength = first.length;
        for (byte[] array : rest) {
            totalLength += array.length;
        }
        byte[] result = Arrays.copyOf(first, totalLength);
        int offset = first.length;
        for (byte[] array : rest) {
            System.arraycopy(array, 0, result, offset, array.length);
            offset += array.length;
        }
        return result;
    }
                        
    public static void main(String[] args) {
        String []a = {"a", "aa"};
        String []b = {"b", "bb"};
        String []c = {"c", "cc"};
        String []arr1 = concat(a, b);
        for (String string : arr1) {
            System.out.println(string);
        }
        String []arr2 = concat(a, b);
        for (String string : arr2) {
            System.out.println(string);
        }
        String []arr3 = concatAll(a, b, c);
        for (String string : arr3) {
            System.out.println(string);
        }
    }
}
